package cn.com.ayqu.module.erp.service.statistics;

import cn.com.ayqu.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.com.ayqu.module.erp.dal.dataobject.product.ErpProductDO;
import cn.com.ayqu.module.erp.dal.dataobject.sale.ErpSaleOutItemDO;
import cn.com.ayqu.module.erp.dal.dataobject.sale.ErpSaleReturnItemDO;
import cn.com.ayqu.module.erp.dal.mysql.sale.ErpSaleOrderMapper;
import cn.com.ayqu.module.erp.dal.mysql.sale.ErpSaleReturnItemMapper;
import cn.com.ayqu.module.erp.dal.mysql.sale.ErpSaleReturnMapper;
import cn.com.ayqu.module.erp.dal.mysql.statistics.ErpSaleStatisticsMapper;
import cn.com.ayqu.module.erp.service.product.ErpProductService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * ERP 销售统计 Service 实现类
 *
 * @author 芋道源码
 */
@Service
public class ErpSaleStatisticsServiceImpl implements ErpSaleStatisticsService {

    @Resource
    private ErpSaleStatisticsMapper saleStatisticsMapper;

    @Resource
    private ErpProductService productService;

    @Resource
    private ErpSaleReturnItemMapper saleReturnItemMapper;

    @Resource
    private ErpSaleOrderMapper saleOrderMapper;

    /**
     * 销售价 = 优惠后金额
     *
     * @param beginTime >= 开始时间
     * @param endTime < 结束时间
     * @return
     */
    @Override
    public BigDecimal getSalePrice(LocalDateTime beginTime, LocalDateTime endTime) {
        return saleStatisticsMapper.getSalePrice(beginTime, endTime);
    }

    /**
     * 利润 = （商品原价 * 数量） - （进货价 * 数量）+ 工时费 - 收款优惠
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public BigDecimal getSaleProfit(LocalDateTime beginTime, LocalDateTime endTime) {
        List<Map<String, Object>> saleProductList = saleStatisticsMapper.getSaleProfit(beginTime, endTime);

        BigDecimal saleProfit = BigDecimal.ZERO;

        for (Map<String, Object> productMap : saleProductList) {
            // 判断是否有销售退货
            List<ErpSaleReturnItemDO> returnItemList = saleReturnItemMapper.selectList(new LambdaQueryWrapperX<ErpSaleReturnItemDO>()
                    .eq(ErpSaleReturnItemDO::getOrderItemId, productMap.get("orderItemId"))
                    .eq(ErpSaleReturnItemDO::getProductId, productMap.get("productId"))
            );

            BigDecimal productCount = BigDecimal.valueOf(Double.parseDouble(productMap.get("count").toString())),
            purchasePrice = BigDecimal.valueOf(Double.parseDouble(productMap.get("purchasePrice").toString())),
            productPrice = BigDecimal.valueOf(Double.parseDouble(productMap.get("productPrice").toString()));
            if (!returnItemList.isEmpty()) {
                productCount = productCount.subtract(returnItemList.stream().map(ErpSaleReturnItemDO::getCount).reduce(BigDecimal.ZERO, BigDecimal::add));
            }

            saleProfit = saleProfit.add(productPrice.multiply(productCount).subtract(purchasePrice.multiply(productCount)));
        }
        BigDecimal workPrice = saleOrderMapper.selectWorkPrice(beginTime, endTime);

        BigDecimal discountPrice = saleStatisticsMapper.getSaleDiscountPrice(beginTime, endTime);

        return saleProfit.add(workPrice).subtract(discountPrice);
    }

}
